<template>
  <div>
    <template v-for="(temp, index) in list">
      <archvieCard :archive="temp" :index="index" :key="index" @btnTouch="btnTouchHandler" @delete="deleteArchive" />
    </template>
  </div>
</template>

<script>
// api
import { delArchiveAPI } from '@/api/archive';
// compoents
import archvieCard from '@/components/ArchiveCard/index.vue';

export default {
    components: {
        archvieCard,
    },
    props: {
        list: {
            type: Array,
            default() {
                return [];
            },
        },
    },
    computed: {
        editorName() {
            return this.$route.meta.editorName;
        },
    },
    methods: {
        btnTouchHandler(Bcode, archive) {
            console.log('点击了第%s个按钮', Bcode);
            if (Bcode === 1) { // 点击第一个按钮 即为编辑
                this.editArchive(archive);
            }
        },
        async deleteArchive(aid, archive) {
            const { code, message } = await delArchiveAPI(aid);
            this.$message({ type: code === 200 ? 'success' : 'error', message });
            archive.isDelete = true;
            this.deleteHandler();
        },
        async deleteHandler() {
            this.$emit('delete');
        },
        async editArchive(archive) {
            if (archive.state === 1) {
                this.$message.error('审核中无法编辑 (｀・ω・´)');
                return;
            }
            this.$router.push({ name: this.editorName, params: { aid: archive.aid } });
        },
    },
};
</script>
